package keke.ai.code.mother.handle.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import keke.ai.code.mother.common.ResultUtils;
import keke.ai.code.mother.enums.SessionEnum;
import keke.ai.code.mother.exception.ErrorCode;
import keke.ai.code.mother.util.WriteJson;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;
import java.util.Objects;

/**
 * 退出登录成功处理器
 */
public class LogoutSuccessHandleImpl implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Object attribute = request.getSession().getAttribute(SessionEnum.LOGIN_USER.getCode());

        if(Objects.isNull(attribute)) {
            WriteJson.writeJson(request, response, ResultUtils.error(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMessage()));
        }

        // 退出登录态
        request.getSession().removeAttribute(SessionEnum.LOGIN_USER.getCode());
    }
}
